package com.travel.travelServer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.Permission;
import com.travel.travelServer.service.PermissionService;
import com.travel.travelServer.utils.BaseController;
import com.travel.travelServer.utils.ResponseData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * (Permission)表控制层
 *
 * @author makejava
 * @since 2023-10-24 16:46:48
 */
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PermissionService permissionService;

    @GetMapping
    public ResponseData selectAll(
            @RequestParam Map<String, Object> conditions
    ) {
        Page<Permission> page = new Page<>(Integer.valueOf(conditions.get("page").toString()),Integer.valueOf(conditions.get("size").toString()));
        return ResponseData.success(permissionService.pageByPermission(page, conditions));
    }

    @GetMapping("/{id}")
    public ResponseData selectOne(@PathVariable("id") Long id) {
        return ResponseData.success(permissionService.getPermissionById(id));
    }

    @PostMapping
    public ResponseData insert(@RequestBody Permission permission) {
        return ResponseData.success(permissionService.savePermission(permission));
    }

    @PutMapping
    public ResponseData update(@RequestBody Permission permission) {
        return ResponseData.success(permissionService.updatePermissionById(permission));
    }
    
    @DeleteMapping("/{id}")
    public ResponseData deleteById(@PathVariable("id") Long id) {
        return ResponseData.success(permissionService.removePermissionById(id));
    }

    @PostMapping("/batchDeletePermission")
    public ResponseData delete(@RequestBody Long[] idList) {
        return ResponseData.success(permissionService.removePermissionByIds(Arrays.asList(idList)));
    }

    @GetMapping("/getByLevel/{level}")
    public ResponseData getPermissionByLevel(@PathVariable("level") Integer level) {
        return ResponseData.success(permissionService.getByLevel(level));
    }

    @GetMapping("/treeList")
    public ResponseData getPermissionTreeList() {
        return ResponseData.success(permissionService.getPermissionTreeList());
    }

    @GetMapping("/getByRoleId/{id}")
    public ResponseData getByRoleId(@PathVariable("id") Long id) {
        return ResponseData.success(permissionService.getByRoleId(id));
    }
    
}

